#include "Myprecent.h"
#include <QPainter>
#include <QThread>
#include <iostream>
Myprecent::Myprecent(QWidget *parent):
    QPushButton(parent)
    ,m_degress(90)
{

}

Myprecent::~Myprecent()
{

}

void Myprecent::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    //绘制外部圆
    QPen pen(QColor("#44444F"));
    pen.setColor(QColor(255, 0, 0, 80));
    // 绘制圆形
    pen.setWidth(8);
    int penWidth = pen.width();
    int radius = (qMin(rect().width(), rect().height()) - penWidth) / 2 - 1;
    painter.setPen(pen);
    painter.setBrush(Qt::NoBrush);
    painter.drawEllipse(rect().center(),
                        radius, radius);

    QRect pieRect(rect().center().x() - radius, rect().center().y() - radius, 2 * radius, 2 * radius);
    //绘制进度半径
    pen.setColor(QColor(255, 0, 0));
    painter.setPen(pen);
    painter.drawArc(pieRect, 0*16, -m_degress *16);

    //内部圆的半径
    radius = radius - penWidth / 2;
    pen.setColor(QColor("#44444F"));
    pen.setWidth(1);
    painter.setPen(pen);
    QBrush brush(QColor("#44444F"));
    painter.setBrush(brush);
    painter.drawEllipse(rect().center(), radius, radius);

     // 设置字体
    QFont font("Arial", 24); // 使用 Arial 字体，大小为 24
    painter.setFont(font);

    // 设置字体颜色
    pen.setColor(Qt::white);
    painter.setPen(pen);

    // 绘制文本
    double value = (double)m_degress / 356;
    std::cout << value << std::endl;
    QString percentageString = QString("%1%").arg(value * 100, 0, 'f', 1);
    painter.drawText(pieRect, Qt::AlignCenter, percentageString);

}

void Myprecent::mousePressEvent(QMouseEvent * event)
{
    if(m_degress > 356) return;
    m_degress++;
    update();
}
